<template>
	<el-dialog
	  title="填写"
	  :visible.sync="visible"
	  :before-close="close"
	  :close-on-click-modal="false"
	  width="98%">

    <el-row>
      <el-col :span="1">
        <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
      </el-col>
      <el-col :span="23">
        <el-checkbox-group v-model="checkBoxItem" size="medium" @change="showRow">
          <el-checkbox v-for="item in allItem" :label="item" :key="item.checkItemId">{{item.inspectionItemName}}</el-checkbox>
        </el-checkbox-group>
      </el-col>
    </el-row>

    <el-card style="margin-top: 10px;">
      <div slot="header">
        <span>
        {{ dataForm.checkList.productName }}
        &nbsp;&nbsp;&nbsp;&nbsp;
        {{ dataForm.standard.insideInspectionStandardNum }}
        </span>
        <el-tooltip effect="dark" :content="isFullLast ? '不填充' : '自动填充'" placement="top">
          <el-switch
            style="float: right;padding: 3px 0"
            v-model="isFullLast"
            @change="changeFullLast"
            active-color="#7cbd36"
            inactive-color="#ff4949"
            inactive-text="上一单">
          </el-switch>
        </el-tooltip>
      </div>
      <div>
        <el-table
        	:data="dataForm.items"
          v-loading="dataListLoading"
          ref="dataTable"
        	border
          fit
          highlight-current-row
          style="width: 100%">

          <el-table-column
              label="编号"
              header-align="center"
              align="center">
            <el-table-column
              prop="checkItemId"
              header-align="center"
              align="center"
              label="快捷选项"
              width="100">
            </el-table-column>
          </el-table-column>

          <el-table-column
            prop="inspectionCategoryName"
            header-align="center"
            align="center"
            label="检验项分类"
            width="100">
          </el-table-column>

          <el-table-column
            prop="inspectionItemName"
            header-align="center"
            align="center"
            label="检验项名称"
            width="100">
          </el-table-column>

          <el-table-column
            prop="insideInspectionItemValue"
            header-align="center"
            align="center"
            label="内控标准值"
            width="200">
            <template slot-scope="{row}">
              <span v-html="row.insideInspectionItemValue"></span>
            </template>
          </el-table-column>

          <el-table-column
            prop="itemConclusionMerge"
            header-align="center"
            align="center"
            label="最终检验结果">
            <template slot-scope="{row}">
              <span v-html="row.itemConclusionMerge"></span>
            </template>
          </el-table-column>

          <el-table-column
            v-if="!ys(dataForm.checkList.checkTypeId)"
            prop="conclusions"
            header-align="center"
            label="单个检验结果"
            align="center"
            width="300">
            <template slot-scope="scope">
              <el-form>
                <el-col :span="scope.row.conclusions.length > 1 ? 18 : 21">
                  <el-form-item
                    v-for="(conc, index) in scope.row.conclusions"
                    :key="index"
                    :prop="'scope.row.conclusions.' + index + 'itemConclusion'">
                    <span v-if="conc.textShow" style="color: red">{{ conc.text }}</span>
                    <el-input @change="setItemConclusion(scope.row, conc, scope.$index, index)" v-if="scope.row.isNumber != 0" v-model="conc.itemConclusion" placeholder="单个检验结果"/>
                    <el-input :id="makeId(scope.$index, index)" @change="setItemConclusion(scope.row, conc, scope.$index, index)" v-else v-model="conc.itemConclusionValue" placeholder="单个检验结果(数字)">
                      <template slot="append">
                        <span v-html="conc.itemConclusionUnit"></span>
                      </template>
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="3">
                  <el-form-item>
                    <el-button type="warning" class="pan-btn warning-btn" icon="el-icon-plus" @click="addConclusion(scope.$index)" circle size="mini" />
                  </el-form-item>
                </el-col>
                <el-col :span="3" v-if="scope.row.conclusions.length > 1">
                  <el-form-item>
                    <el-button type="danger" class="pan-btn danger-btn" icon="el-icon-minus" @click="subtractConclusion(scope.$index)" circle size="mini" />
                  </el-form-item>
                </el-col>
              </el-form>
            </template>
          </el-table-column>

          <el-table-column
            v-else
            prop="ysConclusions"
            header-align="center"
            label="各点检验结果"
            align="center"
            width="300">
            <template slot-scope="scope">
              <el-form>
                <el-col :span="scope.row.ysConclusions.length > 1 ? 18 : 21">
                  <el-form-item
                    v-for="(conc, index) in scope.row.ysConclusions"
                    :key="index"
                    :prop="'scope.row.ysConclusions.' + index + 'itemConclusion'">
                    <span v-if="conc.textShow" style="color: red">{{ conc.text }}</span>
                    <el-input @change="setItemConclusion(scope.row, conc, scope.$index, index)" v-if="scope.row.isNumber != 0" v-model="conc.itemConclusion" :placeholder="conc.waterGetName"/>
                    <el-input :id="makeId(scope.$index, index)" @change="setItemConclusion(scope.row, conc, scope.$index, index)" v-else v-model="conc.itemConclusionValue" :placeholder="conc.waterGetName + '(数字)'">
                      <template slot="append">
                        <span v-html="scope.row.insideInspectionItemValueUnit"></span>
                      </template>
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col v-if="scope.row.ysConclusions.length > 1" :span="3">
                  <el-tooltip content="以首个点位填充结果" placement="top">
                    <el-button type="warning" class="pan-btn warning-btn" icon="el-icon-bottom" @click="fullItemConclusion(scope.$index)" circle size="mini" />
                  </el-tooltip>
                </el-col>
                <el-col v-if="scope.row.ysConclusions.length > 1 && scope.row.isNumber == 1" :span="3">
                  <el-tooltip content="全部填充'符合规定'" placement="top">
                    <el-button type="warning" class="pan-btn warning-btn" icon="el-icon-check" @click="fullItemConclusionAllOk(scope.$index)" circle size="mini" />
                  </el-tooltip>
                </el-col>
              </el-form>
            </template>
          </el-table-column>

          <el-table-column
            label="检验判定"
            header-align="center"
            align="center">
            <el-table-column
              prop="itemVerdict"
              header-align="center"
              align="center"
              width="250">
              <template slot="header" slot-scope="scope">
                <el-radio-group @change="changeAllItemVerdict" v-model="allItemVerdict">
                  <el-radio-button :label="0">符合规定</el-radio-button>
                  <el-radio-button :label="1">不符合规定</el-radio-button>
                </el-radio-group>
              </template>
              <template slot-scope="scope">
                <el-radio-group @change="changeItemVerdict(scope.$index)" v-model="scope.row.itemVerdict">
                  <el-radio-button :label="0">符合规定</el-radio-button>
                  <el-radio-button :label="1">不符合规定</el-radio-button>
                </el-radio-group>
              </template>
            </el-table-column>
          </el-table-column>

          <el-table-column
              label="检验人"
              header-align="center"
              align="center">
            <el-table-column
              prop="inspectionUserId"
              header-align="center"
              align="center"
              width="200">
              <template slot="header" slot-scope="scope">
                <el-select v-model="allInspectionUser" value-key="userId" @change="changeAllInspectionUser" filterable placeholder="检验人">
                  <el-option
                    v-for="item in userList"
                    :key="item.userId"
                    :label="item.name + '(' + item.username + ')'"
                    :value="item">
                  </el-option>
                </el-select>
              </template>
              <template slot-scope="scope">
                <el-select v-model="scope.row.inspectionUser" value-key="userId" @change="setInspectionUser(scope.$index)" filterable placeholder="检验人">
                  <el-option
                    v-for="item in userList"
                    :key="item.userId"
                    :label="item.name + '(' + item.username + ')'"
                    :value="item">
                  </el-option>
                </el-select>
              </template>
            </el-table-column>
          </el-table-column>

          <el-table-column
              label="检验时间"
              header-align="center"
              align="center">
            <el-table-column
              prop="time"
              header-align="center"
              align="center"
              width="250">
              <template slot="header" slot-scope="scope">
                <el-date-picker
                  v-model="allTime"
                  type="daterange"
                  @change="setAllTime"
                  range-separator="-"
                  start-placeholder="开始时间"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  end-placeholder="结束时间"
                  :picker-options="pickerOptions"
                  style="width: 100%;">
                </el-date-picker>
              </template>
              <template slot-scope="scope">
                <div v-if="scope.row.itemStatus == 1 && scope.row.startTime != null && scope.row.endTime == null">
                  {{ scope.row.startTime | changeTime }} -
                  <el-date-picker
                    v-model="scope.row.endTime"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    type="date"
                    @change="setTime(scope.$index)"
                    :picker-options="pickerOptions"
                    placeholder="结束日期"
                    style="width: 62%">
                  </el-date-picker>
                </div>
                <div v-else>
                  <el-date-picker
                    v-model="scope.row.time"
                    type="daterange"
                    @change="setTime(scope.$index)"
                    range-separator="-"
                    start-placeholder="开始时间"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    end-placeholder="结束时间"
                    :picker-options="pickerOptions"
                    style="width: 100%;">
                  </el-date-picker>
                </div>
              </template>
            </el-table-column>
          </el-table-column>

          <el-table-column
            prop="itemStatus"
            header-align="center"
            align="center"
            label="保存状态"
            width="80">
            <template slot-scope="{row}">
              <i v-if="row.itemStatus == 2" style="color: #67C23A;font-size: 25px;" class="el-icon-check"></i>
              <i v-else style="color: #F56C6C;font-size: 25px;" class="el-icon-close"></i>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>

    <span slot="footer" class="dialog-footer">
      <el-button type="primary" class="pan-btn primary-btn" @click="close">确 定</el-button>
    </span>
	</el-dialog>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { getUserList,getGroupWriteInfo,groupWrite,getLastCheckForWrite } from '@/api/check'
	import { CODE_OK } from '@/common/js/config'
  import { isBlank,dateCompare } from '@/common/js/utils'
  import { isNumber } from '@/common/js/validate'
	export default {
		data() {
			return {
				visible: false,
        isFullLast: false,
				dataListLoading: false,
				userList: [],
				checkBoxItem: [],
				allItem: [],
				checkAll: false,
				isIndeterminate: true,
				dataForm: {
				  checkList: '',
				  standard: '',
				  items: [{
            conclusions: [],
            ysConclusions: []
				  }]
				},
        allItemVerdict: '',
        allInspectionUser: '',
        allTime: '',
				pickerOptions: {
				  disabledDate(time) {
				    return time.getTime() > Date.now();
				  }
				},
        saveQueue: []
			}
		},
		props: ["childDataForm"],
		created() {
			if (!isBlank(this.childDataForm)) {
				this.dataForm.checkList = this.childDataForm
        this.getInfo()
        this.visible = true
			}
		},
    computed: {
      ...mapGetters([
         'userInfo'
      ])
    },
		methods: {
      changeItemVerdict(index) {
        if (this.ys(this.dataForm.checkList.checkTypeId) && this.dataForm.items[index].ysConclusions.length != 1) {
          this.dataForm.items[index].itemConclusionMerge = this.dataForm.items[index].itemVerdict == 0 ? '符合规定' : '不符合规定'
        }
        this.saveRow(index,true)
      },
      write() {
        if (this.saveQueue.length == 0) {
          return
        }
        let index = this.saveQueue.pop()
        let row = this.dataForm.items[index]
        row.itemConclusionCount = this.ys(this.dataForm.checkList.checkTypeId) ? row.ysConclusions.length : row.conclusions.length
        const data = {
          content: this.ys(this.dataForm.checkList.checkTypeId) ? 'ys' : 'other',
          checkList: this.dataForm.checkList,
          item: row
        }
        groupWrite(data).then(res => {
          if (res.code === CODE_OK) {
            this.$message.success('保存成功')
            this.dataForm.items[index].itemStatus = 2
            for (let i=0,j=this.allItem.length; i<j; i++) {
              if (row.checkItemId === this.allItem[i].checkItemId) {
                this.allItem[i] = row
              }
            }
          }else{
            this.$message.error(res.msg)
          }
          this.write()
        })
      },
      saveRow(index,isOne) {
        if (this.rowVerify(this.dataForm.items[index])) {
          this.saveQueue.push(index)
          if (isOne) {
            this.write()
          }
        }
      },
      changeFullLast() {
        if (this.isFullLast) {
          if (this.ys(this.dataForm.checkList.checkTypeId)) {
            this.$message.warning('若水的上一单与此单的取样点对应不上，不会填充结果')
          }
          const inspectionItemIds = this.dataForm.items.filter(item => {
            return item.itemStatus == 1
          }).map(item => {
            return item.inspectionItemId
          })
          const data = {
            inspectionItemIds: inspectionItemIds,
            checkTypeId: this.dataForm.checkList.checkTypeId
          }
          getLastCheckForWrite(data).then(res => {
            if (res.code === CODE_OK) {
              let lastItems = res.lastItems
              for (let last of lastItems) {
                for (let i=0,j=this.dataForm.items.length; i<j; i++) {
                   if (last.inspectionItemId == this.dataForm.items[i].inspectionItemId) {
                      let isNumber = this.dataForm.items[i].isNumber == 0 ? true : false
                      this.dataForm.items[i].itemConclusionMerge = isNumber ? null : last.itemConclusionMerge
                      if (last.conclusions instanceof Array) {
                        this.dataForm.items[i].conclusions = last.conclusions.map(conc => {
                          conc.checkItemId = this.dataForm.items[i].checkItemId
                          if (isNumber) {
                            conc.itemConclusion = null
                            conc.itemConclusionValue = null
                          }
                          return conc
                        })
                      }else if(last.ysConclusions instanceof Array) {
                        //赛选点位
                        let oldArr = last.ysConclusions
                        for (let x=0,y=this.dataForm.items[i].ysConclusions.length; x<y; x++) {
                          let wgName = this.dataForm.items[i].ysConclusions[x].waterGetName
                          for (let conc of oldArr) {
                            //如果取样点相同并且非数值类型检验
                            if (conc.waterGetName == wgName && !isNumber) {
                              this.dataForm.items[i].ysConclusions[x].itemConclusion = conc.itemConclusion
                              this.dataForm.items[i].ysConclusions[x].itemConclusionValue = conc.itemConclusionValue
                            }
                          }
                        }
                      }else{
                        this.$message.error('填充错误，请联系管理员')
                        return
                      }
                      this.dataForm.items[i].inspectionUserId = last.inspectionUserId
                      this.dataForm.items[i].inspectionUserName = last.inspectionUserName
                      this.$set(this.dataForm.items[i], 'inspectionUser', {
                        userId: last.inspectionUserId,
                        name: last.inspectionUserName
                      })
                      this.dataForm.items[i].startTime = last.startTime
                      this.dataForm.items[i].endTime = last.endTime
                      this.$set(this.dataForm.items[i], 'time', [
                        last.startTime,
                        last.endTime
                      ])
                     break
                   }
                }
              }
            }else{
              this.$message.error(res.msg)
              this.isFullLast = false
            }
          })
        }else{
          //初始化
        }
      },
      showRow(arr) {
        let checkedCount = arr.length
        this.checkAll = checkedCount === this.allItem.length
        this.isIndeterminate = checkedCount > 0 && checkedCount < this.allItem.length
        this.updateItems(arr)
      },
      updateItems(arr) {
        this.dataForm.items = arr
      },
      handleCheckAllChange(val) {
        this.checkBoxItem = []
        if (val) {
          for (let i=0,j=this.allItem.length; i<j; i++) {
            this.checkBoxItem.push(this.allItem[i])
          }
        }
        this.isIndeterminate = false
        let arr = val ? JSON.parse(JSON.stringify(this.allItem)) : []
        this.updateItems(arr)
      },
      getInfo() {
        this.dataListLoading = true
        const params1 = {
          checkId: this.dataForm.checkList.checkId,
          content: this.ys(this.dataForm.checkList.checkTypeId) ? 'ys' : 'other',
          groupId: this.dataForm.checkList.inspectionGroupId,
          productGmp: this.dataForm.checkList.productGmp
        }
        getGroupWriteInfo(params1).then(res => {
          if (res.code === CODE_OK) {
          	this.dataForm.standard = res.info.standard
            this.dataForm.items = res.info.items.map(item => {
              if (item.inspectionUserId != null) {
                this.$set(item, 'inspectionUser', {
                  userId: item.inspectionUserId,
                  name: item.inspectionUserName
                })
              }
              if (item.startTime != null && item.endTime != null) {
                this.$set(item, 'time', [
                  item.startTime,
                  item.endTime
                ])
              }
              return item
            })
            this.init()
          }else{
          	this.$message.error(res.msg)
          }
        })
        const params2 = {
          groupId: this.dataForm.checkList.inspectionGroupId
        }
        getUserList(params2).then(res => {
          if (res.code === CODE_OK) {
            this.userList = res.list
          }else{
            this.$message.error(res.msg)
          }
        })
        this.dataListLoading = false
      },
      init() {
        this.allItem = JSON.parse(JSON.stringify(this.dataForm.items))
        for (let item of this.allItem) {
          this.checkBoxItem.push(item)
        }
        this.checkAll = true
        this.isIndeterminate = false
      },
      changeAllItemVerdict() {
        let isYs = this.ys(this.dataForm.checkList.checkTypeId)
        for (let i=0,j=this.dataForm.items.length; i<j; i++) {
          if (this.dataForm.items[i].itemStatus == 1) {
            this.dataForm.items[i].itemVerdict = JSON.parse(JSON.stringify(this.allItemVerdict))
            if (isYs && this.dataForm.items[i].ysConclusions.length != 1) {
              this.dataForm.items[i].itemConclusionMerge = this.allItemVerdict == 0 ? '符合规定' : '不符合规定'
            }
            this.saveRow(i,false)
          }
        }
        this.write()
      },
      setInspectionUser(index) {
        this.dataForm.items[index].inspectionUserId = this.dataForm.items[index].inspectionUser.userId
        this.dataForm.items[index].inspectionUserName = this.dataForm.items[index].inspectionUser.name
        this.saveRow(index,true)
      },
      changeAllInspectionUser() {
        for (let i=0,j=this.dataForm.items.length; i<j; i++) {
          if (this.dataForm.items[i].itemStatus == 1) {
            this.$set(this.dataForm.items[i],'inspectionUser',{
              userId: this.allInspectionUser.userId,
              name: this.allInspectionUser.name
            })
            this.dataForm.items[i].inspectionUserId = this.dataForm.items[i].inspectionUser.userId
            this.dataForm.items[i].inspectionUserName = this.dataForm.items[i].inspectionUser.name
            this.saveRow(i,false)
          }
        }
        this.write()
      },
      setTime(index) {
        let row = this.dataForm.items[index]
        if (!isBlank(row.time)) {
          this.dataForm.items[index].startTime = row.time[0]
          this.dataForm.items[index].endTime = row.time[1]
          this.saveRow(index,true)
        }else if(!isBlank(row.startTime) && !isBlank(row.endTime)) {
          this.$set(this.dataForm.items[index],'time',[row.startTime, row.endTime])
          this.saveRow(index,true)
        }
      },
      setAllTime() {
        for (let i=0,j=this.dataForm.items.length; i<j; i++) {
          if (this.dataForm.items[i].itemStatus == 1) {
            this.$set(this.dataForm.items[i],'time',JSON.parse(JSON.stringify(this.allTime)))
            this.dataForm.items[i].startTime = this.dataForm.items[i].time[0]
            this.dataForm.items[i].endTime = this.dataForm.items[i].time[1]
            this.saveRow(i,false)
          }
        }
        this.write()
      },
      setItemConclusion(row, conc, i, j) {
        let isYs = this.ys(this.dataForm.checkList.checkTypeId)
        if (row.isNumber == 0) {
          if (!isNumber(conc.itemConclusionValue)) {
            if (isYs) {
              this.dataForm.items[i].ysConclusions[j].itemConclusionValue = null
            }else{
              this.dataForm.items[i].conclusions[j].itemConclusionValue = null
            }
            this.$alert('请重新输入数值类型的检验结果', '提示', {
              confirmButtonText: '确定',
              callback: action => {
                this.$message({
                  type: 'error',
                  message: '保存失败'
                })
              }
            })
            return
          }else{
            let id = '#' + this.makeId(i, j)
            let isOk = true
            let value = conc.itemConclusionValue * 1000 / 1000
            // 判断超下限
            if (!isBlank(row.insideInspectionItemDownValue)) {
              let down = row.insideInspectionItemDownValue * 1000 / 1000
              if (down > value) {
                isOk = false
                $(id).css({"border-color" : "#D08585"})
                if (isYs) {
                  this.dataForm.items[i].ysConclusions[j].text = '提示：下限值 => ' + down
                  this.dataForm.items[i].ysConclusions[j].textShow = true
                }else{
                  this.dataForm.items[i].conclusions[j].text = '提示：下限值 => ' + down
                  this.dataForm.items[i].conclusions[j].textShow = true
                }
              }
            }
            // 判断超上限
            if (!isBlank(row.insideInspectionItemUpValue)) {
              let up = row.insideInspectionItemUpValue * 1000 / 1000
              if (up < value) {
                isOk = false
                $(id).css({"border-color" : "#D08585"})
                if (isYs) {
                  this.dataForm.items[i].ysConclusions[j].text = '提示：上限值 => ' + up
                  this.dataForm.items[i].ysConclusions[j].textShow = true
                }else{
                  this.dataForm.items[i].conclusions[j].text = '提示：上限值 => ' + up
                  this.dataForm.items[i].conclusions[j].textShow = true
                }
              }
            }
            // 如果不是水,在没有超上下限的情况下,判断超偏差
            if (isOk && !isYs && !isBlank(row.offset)) {
              let positiveThreeOffset = row.offset.positiveThreeOffset * 1000 / 1000
              let positiveTwoOffset = row.offset.positiveTwoOffset * 1000 / 1000
              let negativeThreeOffset = row.offset.negativeThreeOffset * 1000 / 1000
              let negativeTwoOffset = row.offset.negativeTwoOffset * 1000 / 1000
              if (value > positiveThreeOffset) {
                isOk = false
                $(id).css({"border-color" : "#D08585"})
                this.dataForm.items[i].conclusions[j].text = '提示：+3倍偏差 => ' + positiveThreeOffset
                this.dataForm.items[i].conclusions[j].textShow = true
              }else if(value > positiveTwoOffset) {
                isOk = false
                $(id).css({"border-color" : "#D08585"})
                this.dataForm.items[i].conclusions[j].text = '提示：+2倍偏差 => ' + positiveTwoOffset
                this.dataForm.items[i].conclusions[j].textShow = true
              }else if(value < negativeThreeOffset) {
                isOk = false
                $(id).css({"border-color" : "#D08585"})
                this.dataForm.items[i].conclusions[j].text = '提示：-3倍偏差 => ' + negativeThreeOffset
                this.dataForm.items[i].conclusions[j].textShow = true
              }else if(value < negativeTwoOffset) {
                isOk = false
                $(id).css({"border-color" : "#D08585"})
                this.dataForm.items[i].conclusions[j].text = '提示：-2倍偏差 => ' + negativeTwoOffset
                this.dataForm.items[i].conclusions[j].textShow = true
              }
            }
            if (isOk) {
              $(id).removeAttr('style')
              if (isYs) {
                this.dataForm.items[i].ysConclusions[j].text = ''
                this.dataForm.items[i].ysConclusions[j].textShow = false
              }else{
                this.dataForm.items[i].conclusions[j].text = ''
                this.dataForm.items[i].conclusions[j].textShow = false
              }
            }
            let pointNum = conc.itemConclusionValue.substring(conc.itemConclusionValue.indexOf(".") + 1, conc.itemConclusionValue.length)
            let length = pointNum.length
            if (conc.itemConclusionValue.indexOf(".") > -1) {
              value = value.toFixed(length)
            }
            if (isYs) {
              this.dataForm.items[i].ysConclusions[j].itemConclusionValue = value
              this.dataForm.items[i].ysConclusions[j].itemConclusion = isBlank(this.dataForm.items[i].insideInspectionItemValueUnit) ?
               (value + '') : (value + this.dataForm.items[i].insideInspectionItemValueUnit + '')
            }else{
              this.dataForm.items[i].conclusions[j].itemConclusionValue = value
              this.dataForm.items[i].conclusions[j].itemConclusion = isBlank(this.dataForm.items[i].insideInspectionItemValueUnit) ?
               (value + '') : (value + this.dataForm.items[i].insideInspectionItemValueUnit + '')
            }
          }
        }
        this.setItemConclusionMerge(i)
        this.saveRow(i,true)
      },
      fullItemConclusionAllOk(index) {
        this.dataForm.items[index].ysConclusions[0].itemConclusion = '符合规定'
        this.fullItemConclusion(index)
      },
      fullItemConclusion(index) {
        let row = this.dataForm.items[index]
        let firstConc = row.ysConclusions[0]
        let c1 = firstConc.itemConclusion
        let c2 = firstConc.itemConclusionValue
        let c3 = firstConc.itemConclusionUnit
        if (isBlank(c1)) {
          this.$message.error('请先输入第一个点位的检验结果')
          return
        }
        for (let i=1,j=row.ysConclusions.length; i<j; i++) {
          this.dataForm.items[index].ysConclusions[i].itemConclusion = c1
          this.dataForm.items[index].ysConclusions[i].itemConclusionValue = c2
          this.dataForm.items[index].ysConclusions[i].itemConclusionUnit = c3
        }
        this.saveRow(index,true)
      },
      setItemConclusionMerge(index) {
        //如果是水，并且只有一个取样点，那么合并结果为单个结果
        if (this.ys(this.dataForm.checkList.checkTypeId)) {
          if (this.dataForm.items[index].ysConclusions.length == 1) {
            this.dataForm.items[index].itemConclusionMerge = this.dataForm.items[index].ysConclusions[0].itemConclusion
          }
          return
        }
        let itemConclusionArr = []
        let conclusions = this.dataForm.items[index].conclusions
        for (let conc of conclusions) {
          if (!isBlank(conc.itemConclusion)) {
            itemConclusionArr.push(conc.itemConclusion)
          }
        }
        this.dataForm.items[index].itemConclusionMerge = itemConclusionArr.join(";")
      },
      rowVerify(row) {
        if (isBlank(row.itemConclusionMerge)) {
          // this.$message.error("检验合并结果不能为空")
          return false
        }
        let arr = this.ys(this.dataForm.checkList.checkTypeId) ? row.ysConclusions : row.conclusions
        for (let conc of arr) {
          if (isBlank(conc.itemConclusion)) {
            // this.$message.error("单个(各点)检验结果均不能为空")
            return false
          }
        }
        if (row.itemVerdict != 0 && row.itemVerdict != 1) {
          // this.$message.error("检验判定不能为空")
          return false
        }
        if (isBlank(row.inspectionUserId)) {
          // this.$message.error("检验人不能为空")
          return false
        }
        if (isBlank(row.startTime) || isBlank(row.endTime)) {
          // this.$message.error("检验时间不能为空")
          return false
        }else{
          if (!dateCompare(this.dataForm.checkList.dispenseDate, row.startTime)) {
            this.$message.error("检验开始日期必须晚于分样日期")
            return false
          }
        }
        return true
      },
      addConclusion(index) {
        let copy = JSON.parse(JSON.stringify(this.dataForm.items[index].conclusions[0]))
        copy.itemConclusion = null
        copy.itemConclusionValue = null
        this.dataForm.items[index].conclusions.push(copy)
        this.saveRow(index,true)
      },
      subtractConclusion(index) {
        this.dataForm.items[index].conclusions.pop()
        this.setItemConclusionMerge(index)
        this.saveRow(index,true)
      },
			close() {
        if (this.saveQueue.length != 0) {
          this.$alert('正在保存，请稍等')
          return
        }
				this.$emit('close')
				this.$emit('refreshData')
				this.visible = false
			},
      makeId(i,j) {
        return 'conc-id' + i + '-' + j
      }
    }
	}
</script>

<style scoped>
  .el-form-item {
    margin-bottom: 2px !important;
  }
  label {
    margin-bottom: 1px !important;
  }
</style>
